<?php
if ( !defined( 'XAJAX_CCDS_FUNCTION' ) )
    define( 'XAJAX_CCDS_FUNCTION', 'ccds_function' );
require_once dirname( __FILE__ ) . '/ccdsXajaxRequestFunction.inc.php';

class ccdsResponsePlugin
    extends xajaxRequestPlugin
{

    private $sXajaxPrefix="xajax_";
    private $sRequestedFunction=NULL;

    private $sRequestedClass=NULL;

    public function ccdsResponsePlugin( )
    {
        $this->sDefer='';
        $this->sJavascriptURI='';
        $this->bInlineScript=false;

        if ( isset( $_GET['ccdsfun'] ) )
            $this->sRequestedFunction=$_GET['ccdsfun'];

        if ( isset( $_POST['ccdsfun'] ) )
            $this->sRequestedFunction=$_POST['ccdsfun'];

        if ( isset( $_GET['ccdsclass'] ) )
            $this->sRequestedClass=$_GET['ccdsclass'];

        if ( isset( $_POST['ccdsclass'] ) )
            $this->sRequestedClass=$_POST['ccdsclass'];
    }

    public function canProcessRequest( )
    {
        if ( NULL == $this->sRequestedFunction || NULL == $this->sRequestedClass )
            return false;

        return true;
    }

    public function generateClientScript( )
    {
        if ( 0 < count( $this->aFunctions ) )
        {
            echo "\n<script type='text/javascript' " . $this->sDefer . "charset='UTF-8'>\n";

            echo "/* <![CDATA[ */\n";

            foreach ( array_keys( $this->aFunctions ) as $sKey )
                $this->aFunctions[$sKey]->generateClientScript( $this->sXajaxPrefix );

            echo "/* ]]> */\n";

            echo "</script>\n";
        }
    }

    function register( $aArgs )
    {
        if ( 1 < count( $aArgs ) )
        {
            $sType=$aArgs[0];

            if ( XAJAX_CCDS_FUNCTION == $sType )
            {
                $xuf=&$aArgs[1];

                if ( false === is_a( $xuf, 'ccdsXajaxRequestFunction' ) )
                    $xuf=new xajaxCcdsFunction( $xuf, $aArgs[2] );

                if ( 3 < count( $aArgs ) )
                    if ( is_array( $aArgs[3] ) )
                        foreach ( $aArgs[3] as $sName => $sValue )
                            $xuf->configure( $sName, $sValue );

                $this->aFunctions[]=&$xuf;

                return $xuf->generateRequest( $this->sXajaxPrefix );
            }
        }

        return false;
    }

    public function processRequest( )
    {
        if ( NULL == $this->sRequestedFunction )
            return false;

        //header("Content-Type:text/html;\n\n");
        $objArgumentManager=&xajaxArgumentManager::getInstance();
        $aArgs=$objArgumentManager->process();

        $objModuleManager=ccdsModuleManager::getInstance();
        $foo=$objModuleManager->get( $this->sRequestedClass );

        foreach ( array_keys( $this->aFunctions ) as $sKey )
        {
            $xuf=&$this->aFunctions[$sKey];

            if ( $xuf->getName() == $this->sRequestedFunction )
            {
                $sMethod=$xuf->getName();
                $objResponseManager=&xajaxResponseManager::getInstance();
                $objResponseManager->append( $tmp=call_user_func_array( array
                    (
                    &$foo,
                    $sMethod
                    ), $aArgs ) );

                return true;
            }
        }

        return ' Invalid function request received; no request processor found with this name.';
    }
}

$objPluginManager=&xajaxPluginManager::getInstance();
$objPluginManager->registerPlugin( new ccdsResponsePlugin(), 100 );
